var util = require('../../../utils/util.js');
var app = getApp();
/**
 * 根据查询出当前楼栋下可以抄表的房间数量
 */
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ishidden: false,
    roomList:[],//房间列表
    showModalStatus: false,
    readingList:[],
    ishidden:false,
 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    wx.showLoading({
      title: '加载中...',
    })
    that.setData({
      apartment_id: options.apartment_id,
      apartment_name: options.apartment_name
    })
   wx.setNavigationBarTitle({
      title: options.apartment_name
    })
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        that.setData({
          userInfo: res.data,
         
        })
        util.getReq("/app/sdm/getSelectFjAddress", {
          source: res.data.source,
          uid: res.data.user_id,
          phone_num: res.data.phone_num,
          user_name: res.data.user_name,
          apartId:options.apartment_id
        }, function (datas) {
          wx.hideLoading();
          if (datas.code == 0) {
            var dats = datas;
            let roomList=[];
            let readingList=dats.params.readingList;
            for (let i = 0; i < dats.params.room.length;i++){
              let water=0;
              let dian=0;
              let mei=0;
              let re=0;
              if (readingList){
                for(let j=0;j<readingList.length;j++){
                  if (dats.params.room[i].order_id == readingList[j].order_id && readingList[j].reading_type==5){
                       water = readingList[j].room_id;
                    
                  } else if (dats.params.room[i].order_id == readingList[j].order_id && readingList[j].reading_type == 6){
                    dian = readingList[j].room_id;
                    
                  } else if (dats.params.room[i].order_id == readingList[j].order_id && readingList[j].reading_type == 7) {
                    mei = readingList[j].room_id;
                    
                  } else if (dats.params.room[i].order_id == readingList[j].order_id && readingList[j].reading_type == 8) {
                    re = readingList[j].room_id;
                    
                  }
                }
              }
              roomList.push({
                "order_id": dats.params.room[i].order_id,
                "room_code": dats.params.room[i].room_code,
                "renter_name": dats.params.room[i].renter_name,
                "renter_phone": dats.params.room[i].renter_phone,
                "checked":"false",
                "water": water,
                "dian": dian,
                "mei": mei,
                "re": re
              })

            }
           
           that.setData({
             roomList: roomList,
           
           })
            console.log(roomList);
            if (dats.params.room.length<=0){
              that.setData({
                ishidden: false,
              })
            }else{
              that.setData({
                ishidden: true,
              })
            }
          } else {
            var errMsg = '服务器异常';
            if (datas.desc) {
              errMsg = datas.desc;
            }
            wx.showToast({
              title: errMsg,
            })
          }

        })
      }, fail: function () {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh(true);
  },

  setfun: function () {
    var newArray = [];
    for (let i = 0; i < 100; i++) {
      newArray.push({
        "state": 2,
        "key": i + 1,
        "title": "aaa",
        "year": "111",
        "month": "222",
        "date": "333",
      })

    }
    return newArray;
  },
onShow:function(){
  var that = this;
  that.setData({
    items: that.setfun()
  });
},
/**
 * 单选
 */
  checkOne: function (e) {
    var that = this;
    let checkboxItems = that.data.roomList;
    var values = e.detail.value;
    for (var i = 0; i < checkboxItems.length; ++i) {
      checkboxItems[i].checked = false;
      for (var j = 0; j < values.length; ++j) {
        if (checkboxItems[i].order_id == values[j]) {
          checkboxItems[i].checked = true;
          break;
        }
      }
      
    }
    that.setData({
      roomList: checkboxItems,
    })
  },
  /**
 * 全选
 */
  selectalltap: function (e) {    //
    var value = e.detail.value;
    var that = this;
    var checkAll = false;
    if (value && value[0]) {
      checkAll = true;
      let checkboxItems = that.data.roomList;
      for (var i = 0; i < checkboxItems.length; ++i) {
        checkboxItems[i].checked = true;

      }
      that.setData({
        roomList: checkboxItems,
      })
    } else {
      let checkboxItems = that.data.roomList;
      for (var i = 0; i < checkboxItems.length; ++i) {
        checkboxItems[i].checked = false;

      }
      that.setData({
        roomList: checkboxItems,
      })
    }
    that.setData({
      'checkAll': checkAll,
    });

  },
  /**
   * 弹出层动画
   */
  powerDrawer: function (e) {
     //var currentStatu ='open';// e.currentTarget.dataset.statu
    //this.util(currentStatu)
    console.log(e.currentTarget.dataset.roomcode);
    wx.navigateTo({
      url: '/pages/home/roomSign/roomSign_detail/roomSign_detail?orderId=' + e.currentTarget.dataset.key + "&apartment_id=" + this.data.apartment_id + "&apartment_name=" + this.data.apartment_name + "&roomCode=" + e.currentTarget.dataset.roomcode,
    })
  },
  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例 
    var animation = wx.createAnimation({
      duration: 200,  //动画时长
      timingFunction: "linear", //线性
      delay: 0  //0则不延迟
    });

    // 第2步：这个动画实例赋给当前的动画实例
    this.animation = animation;

    // 第3步：执行第一组动画
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画
    setTimeout(function () {
      // 执行第二组动画
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象
      this.setData({
        animationData: animation
      })

      //关闭
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)

    // 显示
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  }

})